package com.hex.ds.hdtp.core.web.data.contrast.controller;

import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastRunLogRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastRunLogService;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.converter.ICrossDbContrastRunLogVoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.vo.query.CrossDbContrastRunLogQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.contrast.controller
 * @ClassName CrossDbContrastRunLogController
 * @Description: 跨库对比记录-WEB接口
 * @Author: wz.li
 * @Date 2023/7/28 18:01
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "05-跨库比对：记录")
@RequestMapping("/api/cross/db/contrast/run/log")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastRunLogController {

    private final ICrossDbContrastRunLogService crossDbContrastRunLogService;

    private final ICrossDbContrastService crossDbContrastService;

    private final ICrossDbContrastRunLogVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastTaskQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库对比记录-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 18:00 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("跨库对比记录-分页查询")
    public ResponseEntity queryListByPage(CrossDbContrastRunLogQueryVo crossDbContrastRunLogQueryVo, PageVo pageVo) {
        log.debug("进入到【CrossDbContrastRunLogController.queryListByPage】方法,参数：{}" + crossDbContrastRunLogQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        CrossDbContrastRunLogRequestDto requestDto = converter.toRequestDtoByQuery(crossDbContrastRunLogQueryVo);
        return ResponseEntity.ok(crossDbContrastRunLogService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @Method stop
     * @Param taskId
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 停止比对记录任务
     * @Author gj.xu
     * @Date 2024/1/11 19:05
     * @Version V2.0
     */
    @GetMapping("/stop/{pkId}/{taskPkId}")
    @ApiOperation("停止比对记录任务")
    public ResponseEntity<Object> stop(@PathVariable("pkId") @NotNull String pkId, @PathVariable("taskPkId") @NotNull String taskPkId) {
        log.debug("进入到【CrossDbContrastRunLogController.stop】方法,主键：{}" + pkId);
        crossDbContrastRunLogService.modifyStopTask(pkId);
        crossDbContrastService.checkTaskFinish(taskPkId);
        return ResponseEntity.ok("停止比对成功");
    }
}
